<template>
    <LDrawerForm
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="drawerForm" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @changeEventHandler="changeEventHandler">
    </LDrawerForm>

</template>

<script>
import LDrawerForm from '@/components/lt/form/LDrawerForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'

export default {
    name:'packageForm',
    components:{
        LDrawerForm
    },
    data(){
        return{
            title:'详情',
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//collapse,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'packageNo',label:'快递单号',component:'input',rules: [{required: false, message: '不能为空'}],disabled:true},
                    {field:'orderTime',label:'下单时间',component:'time',rules: [{required: false, message: '不能为空'}],disabled:true},
                    {field:'startStation',label:'发运站',component:'select',rules: [{required: true, message: '不能为空'}],dictCode:defaultSettings.数据字典.客运站下拉},
                    {field:'endStation',label:'到达站',component:'select',rules: [{required: true, message: '不能为空'}],dictCode:defaultSettings.数据字典.客运站下拉},
                    {field:'senderName',label:'托运人姓名',component:'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'senderPhone',label:'托运人电话',component:'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'consigneeName',label:'收件人姓名',component:'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'consigneePhone',label:'收件人电话',component:'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'goodsType',label:'物品类型',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.物品类型},
                    {field:'goodsName',label:'物品名称',component:'input',rules: [{required: true, message: '不能为空'}]},
                    {field:'goodsNum',label:'物品数量',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'goodsWeight',label:'物品重量(KG)',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {
                        field:['goodsLong','goodsWidth','goodsHeight'],label:'体积CM(长,宽,高)',component:{goodsLong:'inputNumber',goodsWidth:'inputNumber',goodsHeight:'inputNumber'},
                    },
                    {field:'goodsPhoto',label:'物品照片',component:'imageUp',data:{filePath:'goodsPicture'},listType:'picture-card'},
                    {field:'payMode',label:'付款方式',component:'select',rules: [{required: true, message: '不能为空'}],dictCode:defaultSettings.数据字典.付款方式下拉},
                    {field:'goodsFreight',label:'运费',component:'inputNumber',rules: [{required: true, message: '不能为空'}]},
                    {field:'outerPackType',label:'包装类型',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.包装类型},
                    {field:'outerPackNum',label:'包装数量',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'insureStatus',label:'是否保价',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.是否},
                    {field:'declarationValue',label:'声明价值',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'insureValue',label:'保价费',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'storageCost',label:'保管费',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'discountValue',label:'新人优惠',component:'inputNumber',rules: [{required: false, message: '不能为空'}]},
                    {field:'signPhoto',label:'签收照片',component:'imageUp',data:{filePath:'goodsPicture'},listType:'picture-card'},
                    


                    {field:'driverNo',label:'司机',component:'select',rules: [{required: false, message: '不能为空'}],disabled:true,disabled:true,dictCode:defaultSettings.数据字典.司机下拉},
                    {field:'plateNo',label:'车牌号',component:'input',rules: [{required: false, message: '不能为空'}],disabled:true,disabled:true},
                    {field:'isInvoice',label:'是否已开票',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.是否,disabled:true},
                    {field:'payState',label:'是否付款',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.是否,disabled:true},
                    {field:'packageState',label:'快递状态',component:'select',rules: [{required: false, message: '不能为空'}],dictCode:defaultSettings.数据字典.快递状态下拉,disabled:true},


                  ]
                },
              ]

            }


        }
    },
    methods:{
       
        edit (record) {
           
            this.visible = true;
            this.model = Object.assign({}, record);
            if(this.model["keyId"]){
                this.$refs.drawerForm.setIsShowBootomButton(false)
            }else{
                this.$refs.drawerForm.setIsShowBootomButton(true)
            }

            this.$nextTick(() => {
                this.$refs.drawerForm.getForm().setFieldsValue(pick(this.model,'packageNo','orderTime','startStation','endStation','senderName',
                'senderPhone','consigneeName','consigneePhone','goodsName','goodsNum','goodsWeight','goodsLong','goodsWidth','goodsHeight','goodsPhoto','payMode',
                'goodsFreight','driverNo','plateNo','isInvoice','payState','packageState','insureValue', 'declarationValue','insureStatus', 'outerPackType',
                'outerPackNum', 'goodsType', 'signPhoto', 'discountValue', 'storageCost'))
            })
        },
        handleOk(){
            const that = this
            this.$refs.drawerForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                    request({url: '/KhyPackageController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.close()
                            that.$message.success(res.message);
                            that.edit(res.result)
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        /**
         * 表单内容发生变化
         */
        changeEventHandler(changeItem){
            console.log(changeItem)
           
        }
    },
    created(){
        
    },
}
</script>